﻿using DevExpress.XtraEditors;
using JZ_UDI.CommonHelper;
using JZ_UDI.DataAccess;
using JZ_UDI.DataAccess.DataEntity;
using NLog;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LogLevel = NLog.LogLevel;

namespace JZ_UDI.Settings
{
    public partial class frmUdiEdit : UIForm    //DevExpress.XtraEditors.XtraForm
    {
        public frmUdiEdit()
        {
            InitializeComponent();
        }

        // 定义委托，在窗体关闭时将Product对象返回给主窗体
        public delegate void FormBClosedEventHandler(object sender, Product result);

        // 定义事件  
        public event FormBClosedEventHandler FormBClosed;

        //编辑对象
        public Product _Product { get; set; }

        private void btnSave_Click(object sender, EventArgs e)
        {

#region 验证
            if (string.IsNullOrEmpty(txtProductName.Text) || txtProductName.Text.Equals("产品名称不能为空！"))
            {
                txtProductName.Text = "产品名称不能为空！";
                txtProductName.ForeColor = Color.Red;
                return;
            }
            if (string.IsNullOrEmpty(cmbPackagingQuantityCode.Text) || cmbPackagingQuantityCode.Text.Equals("包装数量代码不能为空！"))
            {
                cmbPackagingQuantityCode.Text = "包装数量代码不能为空！";
                cmbPackagingQuantityCode.ForeColor = Color.Red;
                return;
            }
           
#endregion

            txtUdiNoCheckCode.Text = cmbPackagingQuantityCode.Text.Trim().ToString() + txtVendorCode.Text.Trim().ToString() + txtSerialNumber.Text.Trim();
            try
            {
                var count = GetUdiCheckCodeHelper.GetUdiCheckCode(txtSerialNumber.Text.Trim(), txtVendorCode.Text.Trim());
                txtUdiHaveCheckCode.Text = cmbPackagingQuantityCode.Text.Trim().ToString() + txtVendorCode.Text.Trim().ToString() + txtSerialNumber.Text.Trim().ToString() + count.ToString();
            }
            catch (Exception ex)
            {
                Global.SaveLogger(LogLevel.Info, Global.logger.Name, ex.Message);
                throw new Exception(ex.Message);
            }

            #region 更新Product
            using (var context = new EFDbContext())
            {
                Product p = context.Products.ToList().Find(o=>o.ID==_Product.ID);
                if (p != null)
                {
                    p.ProductName = txtProductName.Text.Trim();
                    //p.SerialNumber = string.IsNullOrEmpty(txtSerialNumber.Text) ? null : txtSerialNumber.Text.Trim();
                    p.ComponentName = string.IsNullOrEmpty(txtComponentName.Text) ? null : txtComponentName.Text.Trim();
                    p.ProductModel = string.IsNullOrEmpty(txtProductModel.Text) ? null : txtProductModel.Text.Trim();
                    p.ProductSpecification = string.IsNullOrEmpty(txtProductSpecification.Text) ? null : txtProductSpecification.Text.Trim();
                    p.MaterialCode = string.IsNullOrEmpty(txtMaterialCode.Text) ? null : txtMaterialCode.Text.Trim();
                    p.SterilizationMethod = string.IsNullOrEmpty(cmbSterilizationMethod.Text) ? null : cmbSterilizationMethod.Text.Trim();
                    p.ProductCode = string.IsNullOrEmpty(txtProductCode.Text) ? null : txtProductCode.Text.Trim();
                    p.RegistrationNumber = string.IsNullOrEmpty(txtRegistrationNumber.Text) ? null : txtRegistrationNumber.Text.Trim();
                    p.ProductionLicenseNumber = string.IsNullOrEmpty(txtProductionLicenseNumber.Text) ? null : txtProductionLicenseNumber.Text.Trim();
                    p.PackagingQuantityCode = string.IsNullOrEmpty(cmbPackagingQuantityCode.Text) ? null : cmbPackagingQuantityCode.Text.Trim();
                    p.VendorCode = string.IsNullOrEmpty(txtVendorCode.Text) ? null : txtVendorCode.Text.Trim();
                    p.UdiNoCheckCode = string.IsNullOrEmpty(txtUdiNoCheckCode.Text) ? null : txtUdiNoCheckCode.Text.Trim();
                    p.UdiHaveCheckCode = string.IsNullOrEmpty(txtUdiHaveCheckCode.Text) ? null : txtUdiHaveCheckCode.Text.Trim();
                    p.ProductCategory = string.IsNullOrEmpty(cmbProductCategory.Text) ? null : cmbProductCategory.Text.Trim();
                    p.DrainageTubeDiameter = string.IsNullOrEmpty(txtDrainageTubeDiameter.Text) ? null : txtDrainageTubeDiameter.Text.Trim();
                    p.DrainageTubeLength = string.IsNullOrEmpty(txtDrainageTubeLength.Text) ? null : txtDrainageTubeLength.Text.Trim();
                    p.DrainageBallVolume = string.IsNullOrEmpty(txtDrainageBallVolume.Text) ? null : txtDrainageBallVolume.Text.Trim();
                    p.DrainageBagVolume = string.IsNullOrEmpty(txtDrainageBagVolume.Text) ? null : txtDrainageBagVolume.Text.Trim();
                    p.ProductSpecificationNumber = string.IsNullOrEmpty(txtProductSpecificationNumber.Text) ? null : txtProductSpecificationNumber.Text.Trim();
                    p.SurfaceStates=String.IsNullOrEmpty(cmbSurfaceStates.Text)?null:cmbSurfaceStates.Text.Trim();
                    p.Description = String.IsNullOrEmpty(txtDescription.Text) ? null : txtDescription.Text.Trim();
                    int count = context.SaveChanges();
                    if (count <= 0)
                    {
                        Global.SaveLogger(LogLevel.Info, Global.logger.Name, "UDI更新失败!ID:" + p.ID);
                        throw new Exception("UDI更新失败! ID："+p.ID);
                    }
                    else
                    {
                        // 触发事件，传递结果给订阅者  
                        FormBClosed?.Invoke(this, p);
                    }
                }
                else
                {
                    throw new Exception("UDI更新失败!未找到对象");
                }
            }
            #endregion
            Global.SaveLogger(LogLevel.Info, Global.logger.Name, "更新UDI信息,ID：" +_Product.ID);
            this.DialogResult = DialogResult.OK;
        }

        private void txtProductName_MouseDown(object sender, MouseEventArgs e)
        {
            if (txtProductName.Text.Trim().Equals("产品名称不能为空！"))
            {
                txtProductName.Text = "";
                txtProductName.ForeColor = Color.Black;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Global.SaveLogger(LogLevel.Info, Global.logger.Name, "取消更新UDI");
            DialogResult = DialogResult.Cancel;
        }

        private void frmUdiAdd_Load(object sender, EventArgs e)
        {
            txtProductName.Text = _Product.ProductName;
            txtSerialNumber.Text = _Product.SerialNumber;
            txtComponentName.Text = _Product.ComponentName;
            txtProductModel.Text = _Product.ProductModel;
            txtProductSpecification.Text = _Product.ProductSpecification;
            txtMaterialCode.Text = _Product.MaterialCode;
            cmbSterilizationMethod.Text = _Product.SterilizationMethod;
            txtProductCode.Text = _Product.ProductCode;
            txtRegistrationNumber.Text = _Product.RegistrationNumber;
            txtProductionLicenseNumber.Text = _Product.ProductionLicenseNumber;
            cmbPackagingQuantityCode.Text = _Product.PackagingQuantityCode;
            txtVendorCode.Text = _Product.VendorCode;
            txtUdiNoCheckCode.Text = _Product.UdiNoCheckCode;
            txtUdiHaveCheckCode.Text = _Product.UdiHaveCheckCode;
            cmbProductCategory.Text = _Product.ProductCategory;
            txtDrainageTubeDiameter.Text = _Product.DrainageTubeDiameter;
            txtDrainageTubeLength.Text = _Product.DrainageTubeLength;
            txtDrainageBallVolume.Text = _Product.DrainageBallVolume;
            txtDrainageBagVolume.Text = _Product.DrainageBagVolume;
            txtProductSpecificationNumber.Text = _Product.ProductSpecificationNumber;
            cmbSurfaceStates.Text = _Product.SurfaceStates;
            txtDescription.Text= _Product.Description;
        }

        private void txtProjectCode_MouseDown(object sender, MouseEventArgs e)
        {
            if (txtSerialNumber.Text.Trim().Equals("项目代码长度错误!"))
            {
                txtSerialNumber.Text = "";
                txtSerialNumber.ForeColor = Color.Black;
            }
        }

   

    }
}